package com.pan.utils;

import com.pan.entity.PosNameMapper;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

public class ToStringUtils {

    public static String toLineCSV(String[] arr){
        return Arrays.stream(arr).reduce((a,b)->a+","+b).get()+",\n";
    }

    public static <T> String toLine(T object){
        Class<?> objClass = object.getClass();
        String result = objClass.getName()+"{";
        Field[] declaredFields = objClass.getDeclaredFields();
        result += Arrays.stream(declaredFields)
                .peek(field -> field.setAccessible(true))
                .map(field -> {
                    try {
                        return field.getName()+"=\'"+field.get(object)+"\'";
                    } catch (IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                })
                .reduce((f1,f2)->f1+", "+f2).get();
        result+="}";
        return  result;
    }
}
